﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq.SqlClient;
using S9Engine;


public partial class Layout : Parser
{
    private string _categoryType;

    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Cache.SetCacheability(HttpCacheability.NoCache);
        //Response.Cache.SetExpires(DateTime.Now.AddSeconds(- 1));
        //Response.Cache.SetNoStore();
        
        try
        {
            InitAll();

            Session["CurrentPage"] = "Layout.aspx";
            
            if (!CheckLogin()) Response.Redirect("login.aspx");

            _parser.SetCurrentDir("~/html");
            _parser.SetLanguageFile("~/templates/language.txt", _currentLanguage);

            string type = Request["type"] ?? "category-position";
            _categoryType = Request["categoryType"] ?? "news-category";

            //_DR.Refresh(System.Data.Objects.RefreshMode.StoreWins, _DR.Relations);

            LoadFile("layout.html", false);

            LoadLayoutSetting(type);
        }
        catch (Exception ex)
        {
            WriteError(ex);
        }
        finally
        {
            _parser.Parse("Main");
            _parser.Out("Main");
        }
    }

    private void LoadLayoutSetting(string type)
    {
        try
        {
            List<Positions> positions = _DR.Positions.Where(p => p.LanguageCode.Equals(_currentLanguage)).ToList();
            List<Relations> layouts = null;
            Relations layout = null;

            foreach (Positions p in positions)
            {
                _parser.Assign("positionID", p.ID.ToString());
                _parser.Assign("positionName", p.Name);
                _parser.Assign("positionDescription", p.Description);
                _parser.Parse("Main.Content.Table.PositionHeader.PositionHeaderItem");
            }

            _parser.Assign("categoryType", _categoryType);
            _parser.Parse("Main.Content.Table.PositionHeader");

            if (type == "advertise-position")
            {
                _parser.Assign("layoutType", type);
                layouts = _DR.Relations.Where(r => r.Type.Equals(type)).ToList();

                foreach (Advertises a in _DR.Advertises.Where(a => a.LanguageCode.Equals(_currentLanguage)).ToList())
                {
                    _parser.Assign("objectID", a.ID.ToString());
                    _parser.Assign("objectURL", "advertise.aspx?id=" + a.ID.ToString());
                    _parser.Assign("objectName", a.Name);
                    _parser.Assign("position", string.Empty);

                    foreach (Positions p in positions)
                    {
                        layout = layouts.Where(l => l.Child.Equals(a.ID) && l.Parent.Equals(p.ID)).FirstOrDefault();

                        if (layout != null)
                        {
                            _parser.Assign("objectOrder", layout.Order.ToString());
                            _parser.Assign("checked", "checked=\"checked\"");
                        }
                        else
                        {
                            _parser.Assign("objectOrder", "0");
                            _parser.Assign("checked", string.Empty);
                        }

                        _parser.Assign("positionID", p.ID.ToString());
                        _parser.Assign("positionName", p.Name);
                        _parser.Parse("Main.Content.Table.CategoryList.CategoryListItem.PositionCheckBox.PositionCheckBoxItem");
                    }

                    _parser.Parse("Main.Content.Table.CategoryList.CategoryListItem.PositionCheckBox");
                    _parser.Parse("Main.Content.Table.CategoryList.CategoryListItem");
                }
                
                _parser.Assign("objectIndex", "3");
                _parser.Assign("objectType", _parser.GetLabelValue("Advertise", _currentLanguage));

                _parser.Parse("Main.Content.Table.CategoryList");
            }
            else if (type == "category-position")
            {
                _parser.Assign("layoutType", _categoryType + "-position");
                layouts = _DR.Relations.Where(r => r.Type.Equals(_categoryType + "-position")).ToList();

                List<SelectCategory> selectCategory = (from term in _DR.Terms
                                                       join taxonomy
                                                           in _DR.Taxonomy on term.ID equals taxonomy.Term
                                                       where taxonomy.LanguageCode.Equals(_currentLanguage) && taxonomy.Name.Equals(_categoryType)
                                                       select new SelectCategory()
                                                       {
                                                           ID = term.ID,
                                                           Name = term.Name,
                                                           Parent = taxonomy.Parent
                                                       }).ToList();

                BuildSelectCategory = (ParentId, Level) =>
                {
                    return (from c in selectCategory
                            where c.Parent == ParentId
                            from t in new List<SelectCategory>()
                        {
                            new SelectCategory()
                            {
                                ID = c.ID,
                                Name = new String('-', Level).Replace("-", "&mdash; ") + c.Name,
                                Parent = c.Parent
                            }
                        }.Union(BuildSelectCategory(c.ID, Level + 1))
                            select t).ToList();
                };

                selectCategory = BuildSelectCategory(0, 0);

                foreach (SelectCategory s in selectCategory)
                {
                    _parser.Assign("objectID", s.ID.ToString());
                    _parser.Assign("objectURL", "category.aspx?id=" + s.ID.ToString());
                    _parser.Assign("objectName", s.Name);
                    _parser.Assign("position", string.Empty);

                    foreach (Positions p in positions)
                    {
                        layout = layouts.Where(l => l.Child.Equals(s.ID) && l.Parent.Equals(p.ID)).FirstOrDefault();

                        if (layout != null)
                        {
                            _parser.Assign("objectOrder", layout.Order.ToString());
                            _parser.Assign("checked", "checked=\"checked\"");
                        }
                        else
                        {
                            _parser.Assign("objectOrder", "0");
                            _parser.Assign("checked", string.Empty);
                        }

                        _parser.Assign("positionID", p.ID.ToString());
                        _parser.Assign("positionName", p.Name);
                        _parser.Parse("Main.Content.Table.CategoryList.CategoryListItem.PositionCheckBox.PositionCheckBoxItem");
                    }

                    _parser.Parse("Main.Content.Table.CategoryList.CategoryListItem.PositionCheckBox");
                    _parser.Parse("Main.Content.Table.CategoryList.CategoryListItem");
                }

                _parser.Assign("objectIndex", "5");
                _parser.Assign("objectType", _parser.GetLabelValue("Categories", _currentLanguage));

                _parser.Parse("Main.Content.Table.CategoryList");
                _parser.Parse("Main.Content.CategoryType");
            }

            _parser.Parse("Main.Content.Table");
            _parser.Parse("Main.Content");

            //_parser.Assign("errorCode", Request.Url.ToString());
        }
        catch (Exception ex)
        {
            WriteError(ex);
        }
    }
}